﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace employee.Controls
{
    public partial class emDocFileDetail : common.controls.baseUserControl
    {
        public emDocFileDetail()
        {
            InitializeComponent();
            fileIdEd.BackColor = common.Settings.myTheme.Info.BackColor;
            fileNameEd.BackColor = common.Settings.myTheme.Info.BackColor;
            sizeEd.BackColor = common.Settings.myTheme.Info.BackColor;

            openFileDialog.Filter = common.SysLibs.MakeFileDialogFilter_ImageAndDocument();
        }

        protected void SetDataSource(BindingSource dataSrc)
        {
            common.BindingLibs.BindObject(this.fileIdEd, dataSrc, DataAccess.DataCache.myBaseDS.emFile.fileIdColumn.ColumnName);
            common.BindingLibs.BindObject(this.fileNameEd, dataSrc, DataAccess.DataCache.myBaseDS.emFile.fileNameColumn.ColumnName);
            common.BindingLibs.BindObject(this.fileDescEd, dataSrc, DataAccess.DataCache.myBaseDS.emFile.descriptionColumn.ColumnName);
            common.BindingLibs.BindObject(this.sizeEd, dataSrc, DataAccess.DataCache.myBaseDS.emFile.sizeKBColumn.ColumnName);

            fileNameEd.MaxLength = DataAccess.DataCache.myBaseDS.emFile.fileNameColumn.MaxLength;
            fileDescEd.MaxLength = DataAccess.DataCache.myBaseDS.emFile.descriptionColumn.MaxLength;
        }
        
        public virtual void Init(BindingSource dataSrc) 
        {
            SetDataSource(dataSrc);
        }
        public virtual bool CheckData()
        {
            bool retVal = true;
            ClearNotifyError();
            //if (fileNameEd.Text.Trim() == "") 
            //{
            //    NotifyError(fileNameLbl);
            //    retVal = false;
            //}
            //if (fileDescEd.Text.Trim() == "")
            //{
            //    NotifyError(fileNameLbl);
            //    retVal = false;
            //}
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            fileDescEd.ReadOnly = state;
            openBtn.Enabled = !state;
            scanBtn.Enabled = !state;
            uploadBtn.Enabled = !state;
            downLoadbtn.Enabled = !state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            selectFileNameEd.Focus();
        }

        private void uploadBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (!common.FileLibs.FileExist(selectFileNameEd.Text))
                {
                    common.SysLibs.ShowErrorMessage("Không có file.");
                    selectFileNameEd.Focus();
                }
                if (fileDescEd.Text.Trim() == "") fileDescEd.Text = common.FileLibs.FileNameOnly(selectFileNameEd.Text);
                this.sizeEd.Value = (int)(common.FileLibs.FileSize(this.selectFileNameEd.Text) / 1024);
                this.fileNameEd.Text = application.SysLibs.UploadFile(this.selectFileNameEd.Text);
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }

        private void downLoadbtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (fileIdEd.Text.Trim() == "")
                {
                    common.SysLibs.ShowErrorMessage("Không có file."); return;
                }
                string tmp = application.SysLibs.DownloadFile(fileNameEd.Text);
                common.SysLibs.ShowMessage("Đã lưu vào tệp " + tmp);
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.openFileDialog.ShowDialog() != DialogResult.OK) return;
                this.selectFileNameEd.Text = this.openFileDialog.FileName;
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }

        private void scanBtn_Click(object sender, EventArgs e)
        {
            try
            {
                bool retVal = true;
                string fileName = common.images.ScanFile(out retVal);
                if (retVal && fileName != null) selectFileNameEd.Text = fileName; 
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
    }
}
